package com.wb3.organizer.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.wb3.organizer.dao.AccountDAO;
import com.wb3.organizer.model.Account;

public class AccountDAOImpl implements AccountDAO {
	
	private HibernateTemplate ht;
	
	private static Logger log = Logger.getLogger(AccountDAOImpl.class);
	
	public HibernateTemplate getHt() {
		return ht;
	}

	public void setHt(HibernateTemplate ht) {
		this.ht = ht;
	}

	public void save(Account account) {
		log.info("正在添加新用户：" + account.getUsername() + "......\n");
		ht.save(account);
		log.info("用户：" + account.getUsername() + "添加成功!!!\n");
	}

	public void update(Account account) {
		log.info("正在更新用户：" + account.getUsername() + "......\n");
		ht.update(account);
	}

	public void delete(Account account) {
		log.info("正在删除用户：" + account.getUsername() + "......");
		ht.delete(account);
	}

	@SuppressWarnings("unchecked")
	public Account getAccount(String username) {
		List<Account> accounts = ht.find("from Account a where a.username = ?", username);
		return accounts.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public Account getAccount(int accid){
		List<Account> accounts = ht.find("from Account a where a.userid = ?", accid);
		return accounts.get(0);
	}

	@SuppressWarnings("unchecked")
	public List<Account> getAll() {
		List<Account> accounts = ht.find("from Account");
		return accounts;
	}

}
